package frame;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

import button.Button;
import button.StandardButton;

import constans.Flag;
import constans.Global;
import constans.ResourceManager;
import controllers.PlayerController;
import frame.SkillBar;

public class Frame {

	PlayerController player;
	
	Button[] button= new Button[6];
	SkillBar[] skillBar = new SkillBar[6];
	int flag;
	
	float herosLife;
	Image hp_bar;
	
	Image frame;
	
	int speedLvl = 1;
	int damageLvl = 1;
	int maxLifeLvl = 1;
	int accuracyLvl = 1;
	int regenerationLvl = 1;
	int attackSpeedLvl = 1;
	
	boolean subPoint;
	
	float maxLife;
	float actLife;
	
	public Frame(PlayerController player)
	{
		this.player = player;
		button[0] = new StandardButton(760,83,30,30,Flag.damageButton,ResourceManager.getImage("button"),ResourceManager.getSound("click"));
		skillBar[0] = new SkillBar(new Vector2f(620, 100));
		
		button[1] = new StandardButton(760,123,30,30,Flag.speedButton,ResourceManager.getImage("button"),ResourceManager.getSound("click"));
		skillBar[1] = new SkillBar(new Vector2f(620, 140));
		
		button[2] = new StandardButton(760,163,30,30,Flag.attackSpeedButton,ResourceManager.getImage("button"),ResourceManager.getSound("click"));
		skillBar[2] = new SkillBar(new Vector2f(620, 180));
		
		button[3] = new StandardButton(760,203,30,30,Flag.accuracyButton,ResourceManager.getImage("button"),ResourceManager.getSound("click"));
		skillBar[3] = new SkillBar(new Vector2f(620, 220));
		
		button[4] = new StandardButton(760,243,30,30,Flag.maxLifeButton,ResourceManager.getImage("button"),ResourceManager.getSound("click"));
		skillBar[4] = new SkillBar(new Vector2f(620, 260));
		
		button[5] = new StandardButton(760,283,30,30,Flag.regenerationButton,ResourceManager.getImage("button"),ResourceManager.getSound("click"));
		skillBar[5] = new SkillBar(new Vector2f(620, 300));
		
		
		
		hp_bar = ResourceManager.getImage("hp_bar");
		frame = ResourceManager.getImage("frame");
	}
	public void render(GameContainer container, Graphics g) throws SlickException
	{	
		frame.draw(Global.FRAME_START_X, Global.FRAME_START_Y);
		
		for(Button b : button)
			b.render(container, g);
		for(SkillBar s : skillBar)
			s.render(container, g);
		
		g.setColor(Color.black);		
		g.fillRect(Global.FRAME_START_X + 137, Global.FRAME_START_Y + 580, 40, -150);		
		g.setColor(new Color(157,0,0));		
		g.fillRect(Global.FRAME_START_X + 137, Global.FRAME_START_Y + 580, 40, -herosLife);
		hp_bar.draw(Global.FRAME_START_X + 130, Global.FRAME_START_Y + 430);
		g.setColor(Color.white);
		g.drawString((int)actLife + "/", Global.FRAME_START_X + 135, Global.FRAME_START_Y + 545);
		g.drawString(""+(int)maxLife, Global.FRAME_START_X + 138, Global.FRAME_START_Y + 558);
		g.setColor(Color.white);
		g.drawString("Points: " + player.getPoints(), 620, 40);
		
		g.setColor(Color.black);
		g.drawString("Damage: ", 620, 80);
		g.drawString("Speed: ", 620, 120);
		g.drawString("Attack speed: ", 620, 160);
		g.drawString("Accuracy: ", 620, 200);
		g.drawString("Hitpoints: ", 620, 240);
		g.drawString("Regeneration: ", 620, 280);
		
		
		g.setColor(Color.black);		
		g.fillRect(Global.FRAME_START_X + 25, Global.FRAME_START_Y + 20, 150, 20);		
		g.setColor(Color.blue);	
		g.fillRect(Global.FRAME_START_X + 25, Global.FRAME_START_Y + 20, player.getExperience() * 150 / player.getMaxExperience(), 20);	
	

		
	}
	public void update(GameContainer container, int delta) throws SlickException
	{		
		Input input = container.getInput();
		Vector2f mousePosition = new Vector2f(input.getMouseX(), input.getMouseY());
		updateButtons();
		if(isOn(mousePosition))
		{	
			for(Button b:button)
				{
					b.hover(input);
					if(b.click(input))
					{
						flag = b.getFlag(); 
						updateSkills(flag);
					}
				}
			
		}
	}
	private void updateSkills(int flag2)
	{
		player.subPoint();
		switch(flag2)
		{
			case Flag.damageButton:
				damageLvl++;
				skillBar[0].addSkill();
				break;
			case Flag.speedButton:	
				speedLvl++;
				skillBar[1].addSkill();
				break;
			case Flag.attackSpeedButton:
				attackSpeedLvl++;
				skillBar[2].addSkill();
				break;	
			case Flag.accuracyButton:
				accuracyLvl++;
				skillBar[3].addSkill();
				break;
			
			case Flag.maxLifeButton:
				maxLifeLvl++;
				skillBar[4].addSkill();
				break;
			case Flag.regenerationButton:
				regenerationLvl++;
				skillBar[5].addSkill();
				break;						
		}	
	}
	private void updateButtons()
	{
		if(player.getPoints() < 1)
			for(Button b : button)
			{
				b.setActivity(false);
				b.setVisible(false);
			}
		else
			for(Button b : button)
			{
				b.setActivity(true);
				b.setVisible(true);
			}
		if(damageLvl >= 10)
		{
			button[0].setActivity(false);
			button[0].setVisible(false);
		}
		if(speedLvl >= 10)
		{
			button[1].setActivity(false);
			button[1].setVisible(false);
		}
		if(attackSpeedLvl >= 10)
		{
			button[2].setActivity(false);
			button[2].setVisible(false);
		}
		if(accuracyLvl >= 10)
		{
			button[3].setActivity(false);
			button[3].setVisible(false);
		}
		if(maxLifeLvl >= 10)
		{
			button[4].setActivity(false);
			button[4].setVisible(false);
		}
		if(regenerationLvl >= 10)
		{
			button[5].setActivity(false);
			button[5].setVisible(false);
		}
	}
	public static boolean isOn(Vector2f m)
	{
		if(m.x > Global.FRAME_START_X)
			if(m.x <Global.FRAME_START_X +  Global.FRAME_SIZE_X)
				if(m.y > Global.FRAME_START_Y)
					if(m.y <Global.FRAME_START_Y +  Global.FRAME_SIZE_Y)
						return true;
		return false;
	}
	public int getFlag()
	{
		int tmp = flag;
		flag = Flag.noButton;
		return tmp;
	}
	public void setLife(float l)
	{
		herosLife = l * 1.50f;	
	}
	public void updateLife(float m, float a)
	{
		actLife = a;
		maxLife = m;
	}	
}
